import { api_users_checkLogin } from "@/apis/user";

//用户vuex数据模块

const state = {
	token: localStorage.getItem("token"),
	role: localStorage.getItem("role"),
};
const mutations = {
	SET_TOKEN(state, data) {
		state.token = data;
		localStorage.setItem("token", data);
	},
	SET_ROLE(state, data) {
		state.role = data;
		localStorage.setItem("role", data);
	},
};
const actions = {
	$_Login({ commit }, data) {
		new Promise(async (resolve) => {
			const res = await api_users_checkLogin(data);
			let { code, token, role } = res.data;
			if (code == 0) {
				commit("SET_ROLE", role);
				commit("SET_TOKEN", "Bearer " + token);
				resolve();
			}
		});
	},
};
export default {
	namespaced: true,
	state,
	mutations,
	actions,
};
